﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text;
using System.Xml;

namespace RazakiBLL
{
    public class FeedHelper
    {
        public byte[] GetRSS(string title, string desc, string feedAlternateLink, string feedImageUrl, IEnumerable<SyndicationItem> items)
        {
            string feedId = "FeedID";

            SyndicationFeed feed = new SyndicationFeed(title, desc, new Uri(feedAlternateLink), feedId, DateTime.Now);

            //old Add a custom attribute.
            //XmlQualifiedName xqName = new XmlQualifiedName("CustomAttribute");
            //feed.AttributeExtensions.Add(xqName, "Value");

            //old
            //SyndicationPerson sp = new SyndicationPerson("jesper@contoso.com", "Jesper Aaberg", "http://Jesper/Aaberg");
            //feed.Authors.Add(sp);

            //old
            //SyndicationCategory category = new SyndicationCategory("FeedCategory", "CategoryScheme", "CategoryLabel");
            //feed.Categories.Add(category);

            feed.Contributors.Add(new SyndicationPerson("info@minoosoft.ir", "Minoosoft Group", "www.minoosoft.ir"));
            feed.Copyright = new TextSyndicationContent(string.Format("Copyright {0}", DateTime.Now.Year));
            feed.Description = new TextSyndicationContent("گروه مینوسافت");

            //old Add a custom element.
            //XmlDocument doc = new XmlDocument();
            //XmlElement feedElement = doc.CreateElement("CustomElement");
            //feedElement.InnerText = "Some text";
            //feed.ElementExtensions.Add(feedElement);

            feed.Generator = "اخبار مینوسافت";
            feed.Id = feedId;
            feed.ImageUrl = new Uri(feedImageUrl);

            feed.Items = items;

            feed.Language = "fa-IR";
            feed.LastUpdatedTime = DateTime.Now;

            //old
            //SyndicationLink link = new SyndicationLink(new Uri("http://server/link"), "alternate", "Link Title", "text/html", 1000);
            //feed.Links.Add(link);

            //old
            //XmlWriter atomWriter = XmlWriter.Create("atom.xml");
            //Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(feed);
            //atomFormatter.WriteTo(atomWriter);
            //atomWriter.Close();

            MemoryStream memoryStream = new MemoryStream();
            XmlWriter rssWriter = XmlWriter.Create(memoryStream);
            Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
            rssFormatter.WriteTo(rssWriter);
            rssWriter.Close();

            return memoryStream.GetBuffer();
        }
    }
}
